﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using MySQLStudio.BusinessLogic;
using MySQLStudio.Common;
using MySQLStudio.Common.Data;
using System.Reflection;

namespace MySQLStudio.BusinessLogic.ComponentModel
{
    public class ColumnPropertyDescriptor : PropertyDescriptor
    {
        private Column column;

        public ColumnPropertyDescriptor(string name, Column column, params Attribute[] attrs)
            : base(name, attrs)
        {
            this.column = column;
        }

        public override bool CanResetValue(object component)
        {
            return false;
        }

        public override Type ComponentType
        {
            get
            {
                return typeof(Column);
            }
        }

        public override object GetValue(object component)
        {
            object value = null;

            if (component != null)
            {
                PropertyInfo propertyInfo = this.ComponentType.GetProperty(base.Name);

                if (propertyInfo.PropertyType.IsArray)
                {
                    value = propertyInfo.GetValue(component, null);
                }
                else
                {
                    value = propertyInfo.GetValue(component, null);
                }
            }

            return value;
        }

        public override bool IsReadOnly
        {
            get
            {
                if ((base.Name != "Length" && base.Name != "Precision" && base.Name != "Scale" && base.Name != "Collation")
                    || this.column.DataType == null)
                {
                    return false;
                }
                else
                {
                    DbDataType dataType = DataUtils.DataTypes.FirstOrDefault(dt => dt.Name == this.column.DataType);

                    if (base.Name == "Length")
                    {
                        return !dataType.EnableLength;
                    }
                    else if (base.Name == "Precision")
                    {
                        return !dataType.EnablePrecision;
                    }
                    else if (base.Name == "Scale")
                    {
                        return !dataType.EnableScale;
                    }
                    else if (base.Name == "Collation")
                    {
                        return !dataType.EnableCollation;
                    }

                    return false;
                }
            }
        }

        public override Type PropertyType
        {
            get
            {
                PropertyInfo propertyInfo = this.ComponentType.GetProperty(base.Name);

                if (propertyInfo != null)
                {
                    return propertyInfo.PropertyType;
                }

                throw new InvalidOperationException();
            }
        }

        public override void ResetValue(object component)
        {
            
        }

        public override void SetValue(object component, object value)
        {
            PropertyInfo propertyInfo = this.ComponentType.GetProperty(base.Name);

            if (propertyInfo != null)
            {
                if (DBNull.Value.Equals(value))
                {
                    value = null;
                }

                propertyInfo.SetValue(component, value, null);
            }
        }

        public override bool ShouldSerializeValue(object component)
        {
            return false;
        }
    }
}
